package edu.neu.swc.cloud.controller;

import com.alibaba.fastjson.JSON;
import edu.neu.swc.cloud.domain.Factory;
import edu.neu.swc.cloud.domain.QueryInfo;
import edu.neu.swc.cloud.service.FactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/factory")
public class FactoryController {

    @Autowired
    FactoryService factoryService;

    @RequestMapping("/factoryList")
    public String factoryList(QueryInfo queryInfo){
        System.out.println(queryInfo);
        int numbers = factoryService.getFactoryCounts("%" + queryInfo.getQuery() + "%");
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Factory> factoryList = factoryService.findFactoryByQuery("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",factoryList);
        System.out.println("总条数："+numbers);
        System.out.println(factoryList);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/shutFactory")
    public String shutFactory(int id){
        factoryService.shutDownById(id);
        return "success";
    }

    @RequestMapping("/startFactory")
    public String startFactory(int id){
        factoryService.startUpById(id);
        return "success";
    }
}
